﻿using System;
using System.Diagnostics;
using OutlookContactsSync.Domain.Presenters;
using OutlookContactsSync.Domain.Properties;
using OutlookContactsSync.Domain.Views;
using OutlookContactsSync.Services;

namespace OutlookContactsSync.Presenters
{
    public class OutlookSyncSettingsPresenter : IOutlookSyncSettingsPresenter
    {
        private IOutlookSyncSettingsView _view;

        public OutlookSyncSettingsPresenter(IOutlookSyncSettingsView view)
        {
            View = view;
        }

        #region IOutlookSyncSettingsPresenter Members

        public void InitView()
        {
            View.BusinessChecked = Settings.Default.BusinessChecked;
            View.BusinessFaxChecked = Settings.Default.BusinessFaxChecked;
            View.CompanyChecked = Settings.Default.CompanyChecked;
            View.DisplayAsChecked = Settings.Default.DisplayAsChecked;
            View.EmailChecked = Settings.Default.EmailAsChecked;
            View.FileAsChecked = Settings.Default.FileAsChecked;
            View.FullNameChecked = Settings.Default.FullNameChecked;
            View.HomeChecked = Settings.Default.HomeChecked;
            View.ImAddressChecked = Settings.Default.ImAddressChecked;
            View.JobTitle = Settings.Default.JobTitleChecked;
            View.MobilePhone = Settings.Default.MobilePhoneChecked;
            View.Picture = Settings.Default.PictureChecked;
            View.WebPageAddress = Settings.Default.WebPageAddress;
            View.ContactsCategory = Settings.Default.ContactsCategory;

            View.RemoveOldUsersChecked = Settings.Default.RemoveOldUsers;
            View.ScreenStatus = Domain.Globals.Settings.Status.SettingsUnchanged; //Reset status to unchanged

            View.ShowForm();
            AddEventHandlers();
        }

        public IOutlookSyncSettingsView View
        {
            private get
            {
                Guard.ArgumentNotNull(_view, "view");
                return _view;
            }
            set
            {
                _view = value;
                AddEventHandlers();
            }
        }

        #endregion

        private void AddEventHandlers()
        {
            View.Save += delegate { Save(); };
            View.Reset += delegate { Reset(); };
        }

        private void Save()
        {
            try
            {
                Settings.Default.BusinessChecked = View.BusinessChecked;
                Settings.Default.BusinessFaxChecked = View.BusinessFaxChecked;
                Settings.Default.CompanyChecked = View.CompanyChecked;
                Settings.Default.EmailAsChecked = View.EmailChecked;
                Settings.Default.FileAsChecked = View.FileAsChecked;
                Settings.Default.FullNameChecked = View.FullNameChecked;
                Settings.Default.HomeChecked = View.HomeChecked;
                Settings.Default.ImAddressChecked = View.ImAddressChecked;
                Settings.Default.JobTitleChecked = View.JobTitle;
                Settings.Default.MobilePhoneChecked = View.MobilePhone;
                Settings.Default.PictureChecked = View.Picture;
                Settings.Default.WebPageAddress = View.WebPageAddress;
                Settings.Default.DisplayAsChecked = View.DisplayAsChecked;
                Settings.Default.RemoveOldUsers = View.RemoveOldUsersChecked;
                Settings.Default.ContactsCategory = View.ContactsCategory;

                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Saving app settings failed at: {0}. Exception message: {1} ", DateTime.Now, ex.Message), "Exception");
                View.ExceptionMessage = ex.Message;
            }
        }

        private void Reset()
        {
            View.BusinessChecked = DefaultSettings.Default.DefaultBusinessChecked;
            View.BusinessFaxChecked = DefaultSettings.Default.DefaultBusinessFaxChecked;
            View.CompanyChecked = DefaultSettings.Default.DefaultCompanyChecked;
            View.DisplayAsChecked = DefaultSettings.Default.DefaultDisplayAsChecked;
            View.EmailChecked = DefaultSettings.Default.DefaultEmailAsChecked;
            View.FileAsChecked = DefaultSettings.Default.DefaultFileAsChecked;
            View.FullNameChecked = DefaultSettings.Default.DefaultFullNameChecked;
            View.HomeChecked = DefaultSettings.Default.DefaultHomeChecked;
            View.ImAddressChecked = DefaultSettings.Default.DefaultImAddressChecked;
            View.JobTitle = DefaultSettings.Default.DefaultJobTitleChecked;
            View.MobilePhone = DefaultSettings.Default.DefaultMobilePhoneChecked;
            View.Picture = DefaultSettings.Default.DefaultPictureChecked;
            View.WebPageAddress = DefaultSettings.Default.DefaultWebPageAddress;
            View.RemoveOldUsersChecked = DefaultSettings.Default.DefaultRemoveOldUsers;
            View.ContactsCategory = DefaultSettings.Default.DefaultContactsCategory;
        }
    }
}